﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;

namespace BSInstraAPI.Models
{
    public class UserRepository : IUserRepository
    {
        /// <summary>
        /// 获取全部用户
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public IEnumerable<Users> GetAllUsers(string token)
        {
            if (Token.Check(token))
            {
                string con = "server=192.168.150.60;database=Baison_portal;uid=dbmanager;pwd=jyehFuV27FxNQ50tLOV0gglvPZ0=";
                DataContext dbContext = new DataContext(con);
                IEnumerable<Users> bsUserses = from i in dbContext.GetTable<Users>()
                                               select i;
                return bsUserses;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 按登录名获取用户
        /// </summary>
        /// <param name="token"></param>
        /// <param name="loginid"></param>
        /// <returns></returns>
        public Users Get(string token, string loginid)
        {
            if (Token.Check(token))
            {
                string con = "server=192.168.150.60;database=Baison_portal;uid=dbmanager;pwd=jyehFuV27FxNQ50tLOV0gglvPZ0=";
                DataContext dbContext = new DataContext(con);
                IEnumerable<Users> bsUsers = from i in dbContext.GetTable<Users>()
                                             where i.Loginid == loginid
                                             select i;
                if (bsUsers.Any())
                {
                    return bsUsers.FirstOrDefault();
                }
                else
                {
                    return null;
                }

            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 检查用户密码
        /// </summary>
        /// <param name="token"></param>
        /// <param name="loginid"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public bool Check(string token, string loginid, string pass)
        {
            bool result = false;
            if (Token.Check(token))
            {
                string con = "server=192.168.150.60;database=Baison_portal;uid=dbmanager;pwd=jyehFuV27FxNQ50tLOV0gglvPZ0=";
                DataContext dbContext = new DataContext(con);
                IEnumerable<Users> bsUserses = from i in dbContext.GetTable<Users>()
                                               where i.Loginid == loginid && i.Pswd == BaisonDES.DESEnCode(pass)
                                               select i;
                if (bsUserses.Any())
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="token">令牌</param>
        /// <param name="loginid">登录名</param>
        /// <param name="pswd">密码</param>
        /// <returns></returns>
        public bool Update(string token, string loginid, string pswd)
        {
            bool result = false;
            if (Token.Check(token))
            {
                string con = "server=192.168.150.60;database=Baison_portal;uid=dbmanager;pwd=jyehFuV27FxNQ50tLOV0gglvPZ0=";
                DataContext dbContext = new DataContext(con);

                try
                {
                    Users user =
                   dbContext.GetTable<Users>()
                       .SingleOrDefault(u => u.Loginid == loginid)
                   ;
                    user.Pswd = BaisonDES.DESEnCode(pswd);
                    user.PswdModifyDateTime = DateTime.Now;

                    dbContext.SubmitChanges();
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                    throw;
                }
            }
            return result;
        }

        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="token">令牌</param>
        /// <param name="loginid">登录名</param>
        /// <param name="oPass">旧密码</param>
        /// <param name="nPass">新密码</param>
        /// <returns></returns>
        public bool Update(string token, string loginid, string oPass, string nPass)
        {
            bool result = false;
            if (Token.Check(token))
            {
                string con = "server=192.168.150.60;database=Baison_portal;uid=dbmanager;pwd=jyehFuV27FxNQ50tLOV0gglvPZ0=";
                DataContext dbContext = new DataContext(con);

                try
                {
                    Users user =
                   dbContext.GetTable<Users>()
                       .SingleOrDefault(u => u.Loginid == loginid && u.Pswd == BaisonDES.DESEnCode(oPass))
                   ;
                    user.Pswd = BaisonDES.DESEnCode(nPass);
                    user.PswdModifyDateTime = DateTime.Now;

                    dbContext.SubmitChanges();
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                    throw;
                }
            }
            return result;
        }

  

    }
}